package com.szh.cloud.user.service.impl;

import com.szh.cloud.mybatis.entity.PtUserEntity;
import com.szh.cloud.mybatis.flexigrid.FlexResEntity;
import com.szh.cloud.mybatis.flexigrid.PagingBean;
import com.szh.cloud.mybatis.ztree.ZtreeEntity;
import com.szh.cloud.spring.annotation.LogPoint;
import com.szh.cloud.user.entity.PasswordEntity;
import com.szh.cloud.user.entity.SearchUserEntity;
import com.szh.cloud.user.entity.UserEntity;
import com.szh.cloud.user.execption.RepeatEmailException;
import com.szh.cloud.user.execption.RepeatMobileException;
import com.szh.cloud.user.execption.RepeatUserNameException;
import com.szh.cloud.user.execption.UseNameCcupdException;
import com.szh.cloud.user.repository.UserDao;
import com.szh.cloud.user.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private PasswordEncoder passwordEncoder;
    @LogPoint
    @Override
    public List<PtUserEntity> queryUserInfo(String userName) {
        return userDao.queryUserInfo(userName);
    }

    @Override
    public void deleteUserById(String userUuid) {
        userDao.deleteUserById(userUuid);
    }
    @Value("${ztree.role.manage.id}")
    private String roleManagerId;
    @Value("${ztree.root.id}")
    private String ztreeRootId;
    @Resource
    private MessageSource messageSource;

    @Override
    public FlexResEntity<PtUserEntity> getUserInfoListByPage(SearchUserEntity entity, PagingBean paging) {
        entity.deal();
        paging.deal(PtUserEntity.class);
        FlexResEntity<PtUserEntity> flexResEntity = new FlexResEntity<>();
        flexResEntity.setPage(paging.getPage());
        List<PtUserEntity> ptUserEntityList = userDao.queryUserListByPage(entity,paging);
        Integer total = userDao.queryUserInfoTotal(entity);
        flexResEntity.setRawRecords(ptUserEntityList);
        flexResEntity.setTotal(total);
        flexResEntity.deal();
        return flexResEntity;
    }

    @Override
    public List<ZtreeEntity> getRoleByUserId(String userUuids) {
        List<ZtreeEntity> list = userDao.queryRoleZtreeList();

        Locale locale = LocaleContextHolder.getLocale();
        String managerName = messageSource.getMessage("user.role.allocate.manager",null,locale);

        ZtreeEntity manager = new ZtreeEntity();
        manager.setId(this.roleManagerId);
        manager.setpId(this.ztreeRootId);
        manager.setName(managerName);
        list.add(manager);
        return list;
    }

    @Override
    public void saveRoleByUser(String userUuids, String roleIds) {
        String[] userUuidArray = userUuids.split(",");
        String[] roleIdArray = roleIds.split(",");
        for(String userUuid:userUuidArray){
            userDao.deleteRoleByUserId(userUuid);
            if("".equals(roleIds)){
                continue;
            }
            for(String roleId:roleIdArray){
                userDao.insertRoleRefUser(userUuid,roleId);
            }
        }
    }

    @Override
    public List<ZtreeEntity> getRoleBySingUserId(String userUuid) {
        List<ZtreeEntity> list = userDao.queryRoleZtreeList();

        Locale locale = LocaleContextHolder.getLocale();
        String managerName = messageSource.getMessage("user.role.allocate.manager",null,locale);

        ZtreeEntity manager = new ZtreeEntity();
        manager.setId(this.roleManagerId);
        manager.setpId(this.ztreeRootId);
        manager.setName(managerName);

        List<String> roleIdList = userDao.queryRoleByUserId(userUuid);
        for(ZtreeEntity ztreeEntity :list){
            for(String roleId:roleIdList){
                if(roleId.equals(ztreeEntity.getId())){
                    ztreeEntity.setChecked(true);
                    break;
                }
            }
        }
        list.add(manager);
        return list;
    }

    @Override
    public void addUserInfo(UserEntity userEntity) throws RepeatUserNameException, RepeatEmailException, RepeatMobileException {
        List<PtUserEntity> list = userDao.queryUserByUserName(userEntity.getUserName());
        if(list.size() > 0){
            throw new RepeatUserNameException(userEntity.getUserName() + " repeat");
        }

        list = userDao.queryUserInfoByEmail(userEntity.getEmail());
        if(list.size() > 0){
            throw new RepeatEmailException(userEntity.getEmail() + " repeat");
        }

        list = userDao.queryUserByMobile(userEntity.getMobile());
        if (list.size() > 0 ){
            throw new RepeatMobileException(userEntity.getMobile() + " repeat");
        }

        String password = passwordEncoder.encode(userEntity.getPassword());
        userEntity.setPassword(password);
        userDao.addUserInfo(userEntity);
    }

    @Override
    public PtUserEntity queryUserListById(String userUuid) {
        return userDao.queryUserListById(userUuid);
    }

    @Override
    public void modifyUserInfo(UserEntity userEntity) throws RepeatUserNameException, RepeatEmailException, RepeatMobileException {

        List<PtUserEntity> list = userDao.queryUserByUserNameId(userEntity.getUserName(),userEntity.getUserUuid());
        if(list.size() > 0){
            throw new RepeatUserNameException(userEntity.getUserName() + " repeat");
        }

        list = userDao.queryUserInfoByEmailId(userEntity.getEmail(),userEntity.getUserUuid());
        if(list.size() > 0){
            throw new RepeatEmailException(userEntity.getEmail() + " repeat");
        }

        list = userDao.queryUserByMobileId(userEntity.getMobile(),userEntity.getUserUuid());
        if (list.size() > 0 ){
            throw new RepeatMobileException(userEntity.getMobile() + " repeat");
        }
        userDao.modifyUserInfoById(userEntity);
    }

    @Override
    public void deleteUserRecycleById(String userUuids) throws UseNameCcupdException {
        String[] userUuidArray = userUuids.split(",");
        List<String> roleIdList;
        for (String userUuid:userUuidArray) {
            roleIdList = userDao.queryRoleByUserId(userUuid);
            if(roleIdList.size() > 0){
                throw new UseNameCcupdException("user is exit in role");
            }
            userDao.deleteUserRecycleById(userUuid);
        }

    }

    @Override
    public void modifyPasswordById(PasswordEntity entity) {
        entity.deal();
        String password = entity.getPassword();
        password = passwordEncoder.encode(password);
        entity.setPassword(password);
        userDao.modifyPasswordById(entity);
    }
}